﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PBOffice.DAL;
using PBOffice.BLL;

public partial class PublicInformationModule_userStructure : System.Web.UI.Page
{
    v_UserDepPosition user = new v_UserDepPosition();
    List<v_UserDepPosition> userList = new List<v_UserDepPosition>();
    UserManager userManager = new UserManager();
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
            ShowTreeNode();
        }
    }
    string NodeStyle(v_UserDepPosition user)   //设置节点格式
    {
        string nodeStyle = "<div><a href=\"{0}\">{1}</a>({2})</div><div style=\"font-size:11px;color:#ff6600\">{3}</div>"; //0:链接地址；1：员工姓名；2：用户名；3：用户职位
        return string.Format(nodeStyle,"UserDetails.aspx?UserNo="+user.UserNo,user.UserName,user.UserId,user.PosId);       //节点显示的信息       
    }
    string GetNode( v_UserDepPosition user)    //获取节点信息
    {
        return string.Format("myTree.add({0}, {1}, '{2}');",user.UserNo,user.UserLeaderNo,NodeStyle(user));          //节点详细信息
    }
    /// <summary>
    /// 显示员工树的节点信息
    /// </summary>
    public void ShowTreeNode()
    {
        StringBuilder sb = new StringBuilder();
        int depNo = Convert.ToInt32(Request.QueryString["DepNo"].ToString());    //通过URL传递过来的部门编号
        userList = userManager.ShowUserInfoByDep(depNo);                         //根据部门编号查询员工信息
        var user = from u in userList where u.UserLeaderNo == 0 select u;        //查询上级编号为0的员工信息
        if (user.ToList().Count>0)
        {
            v_UserDepPosition userTop = (v_UserDepPosition)user.First();     //符合条件的第一个用户信息
            if (userTop != null)
            {
                sb.AppendFormat(GetNode(userTop));                          //显示指定格式的节点信息
                ShowChildUser(sb, userTop);                                 //显示下一级员工信息为指定格式

            } 
        }
    }

    /// <summary>
    ///显示下一级员工信息
    /// </summary>
    /// <param name="sb"></param>
    /// <param name="userTop"></param>
    private void ShowChildUser(StringBuilder sb, v_UserDepPosition userTop)
    {
        userList = userManager.ShowUserInfoByUserLeaderNo(userTop.UserNo);                      //根据上级编号查询员工信息
        if (userList.Count > 0)
        {
            foreach (v_UserDepPosition userNew in userList)
            {
                sb.AppendFormat(GetNode(userNew));            //将员工信息显示成指定格式
                ShowChildUser(sb,userNew);            //递归显示员工信息
            }
        }
    }
}